<?php 
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Userapi extends CI_Controller {
	/**
	 * 判断递交方法
	 *
	 * @param	IS_POST	//新增
	 * @param	IS_GET	//查询
	 * @param	IS_PUT  //修改
	 * @param	IS_DEL  //删除
	 *
	 * @param	$this->load->model('Model_m');  // 实例 增删改查 的模型
     * @param	$user_IP = $this->Model_m->select('app_auth',$ip_where,'id desc','1,1');  1//app_auth 数据表  2//$ip_where where数组 3//正序倒序  4//limit
	 * 	
     *
	 */

	public function __construct(){
		parent::__construct();
		header('Content-Type: application/json; charset=utf-8');
		$this->load->model('Model_m');// 通用模型 实例 增删改查 的模型 
		$this->load->model('Userapi_m');// 用户表 实例 增删改查 的模型
		$this->load->model('AppAuth_m');// 第三方 实例 增删改查 的模型
		$this->load->library('api/userapi_service');// 用户中间层 实例 增删改查 的模型
	}
	

    public function user_api(){
   //  	$server_ip = $_SERVER["REMOTE_ADDR"]; //获取到的IP地址 $output = isset($value) ? $value : 'No value set.';
   //  	$appid = isset($_REQUEST['appid'])?$_REQUEST['appid']:'';
    		
   //  	$ip_where = array('appid'=>$appid,'ip'=>$server_ip); //where
   //  	//判断IP地址是否合法  这里用户只跟指定IP交互用户数据
   //  	$user_IP = $this->AppAuth_m->select($ip_where);
    		
   //  	if($user_IP){
			// if($server_ip != $user_IP[0]['ip']){
	  //   		$data['error'] = error(4003);
			// 	echo _echo($data);
		 //    	exit;
			// }	

	  //   	if($appid != $user_IP[0]['appid']){
	  //   		$data['error'] = error(4003);
		 //    	echo _echo($data);
		 //    	exit;
			// }	
   //  	}else{
   //  		$data['error'] = error(4001);
		 //    echo _echo($data);
	  //   	exit;
   //  	}

    	switch ($_SERVER["REQUEST_METHOD"]) {
			case 'GET':
				$action = $this->input->get('action');
				if($action==1){
					//通过用户名密码登录
					$get['username'] = $this->input->get('username');
					$get['password'] = MD5($this->input->get('password'));
					$data = $this->userapi_service->getUserByUsernameAndPassword($get);
					echo _echo($data);
				}
				if($action==2){
					$field = $this->input->get('field');
					$value = $this->input->get('value');
					$data = $this->userapi_service->getUserByinfo($field,$value);
					echo _echo($data);
				}
				if($action==3){
					$get['user_id'] = $this->input->get('user_id');
					$data = $this->userapi_service->user_api_get($get);
					echo _echo($data);
				}					
				break;
			case 'POST':
				$post['username'] = $this->input->post('username');
				$data = $this->userapi_service->user_api_post($post);
				echo _echo($data);
				break;
			case 'PUT':
				$put['user_id'] = $this->input->input_stream('user_id');
				$put['username'] = $this->input->input_stream('username');  
				$put['password'] = $this->input->input_stream('password');  
				$put['mobile'] = $this->input->input_stream('mobile');  
				$put['email'] = $this->input->input_stream('email'); 
				
				$data = $this->userapi_service->user_api_put($put);
					
				echo _echo($data);
				break;
			case 'DELETE':
				$delete['user_id'] = $this->input->input_stream('user_id');
				$data = $this->userapi_service->user_api_delete($delete);
				echo _echo($data);
				break;

			default:
				# code...
				break;
		}
		
    	
		// //添加用户**
		// if(IS_POST){
		// 	//如果是POST提交
		// 	$username = isset($_REQUEST['username'])?$_REQUEST['username']:'';
		// 	$user_where = array('username'=>$username); //where
		// 	//查询有没有这个用户
		// 	$user_old = $this->Model_m->select('users',$user_where);

		// 	if($user_old){
		// 		$user_old_where = array('user_id'=>$user_old[0]['user_id']); //where
		//         $user_old_weixin_where = array('user_id'=>$user_old[0]['user_id']); //where
		//         $data['error'] = $this->Model_m->data_error('5000');
		//     	$data['user'] = $this->Model_m->select('users',$user_old_where,'user_id asc');
		//     	$data['weixin'] = $this->Model_m->select('users_weixin',$user_old_weixin_where,'id asc');
		    	
		//     	echo json_encode($data,JSON_UNESCAPED_UNICODE);
		// 	}else{
		// 		$user_last_where = ''; //where
		// 		$user_last = $this->Model_m->select('users',$user_last_where,'user_id desc','1,1');
		// 		$data['user_id'] = $user_last[0]['user_id']+1;
		// 		$data['username'] = isset($_REQUEST['username'])?$_REQUEST['username']:'';
		// 		$data['password'] = isset($_REQUEST['password'])?MD5($_REQUEST['password']):'';
		// 		$data['mobile'] = isset($_REQUEST['mobile'])?$_REQUEST['mobile']:'';
		// 		$data['email'] = isset($_REQUEST['email'])?$_REQUEST['email']:'';
		// 		if(empty($data['username'])){
		//     		$data['error'] = $this->Model_m->data_error('4007'); 
		// 	    	echo json_encode($data,JSON_UNESCAPED_UNICODE);
		// 	    	exit;
		// 		}	

		// 		if(empty($data['password'])){
		//     		$data['error'] = $this->Model_m->data_error('4007'); 
		// 	    	echo json_encode($data,JSON_UNESCAPED_UNICODE);
		// 	    	exit;
		// 		}

		// 		if(empty($data['mobile'])){
		//     		$data['error'] = $this->Model_m->data_error('4007');
		// 	    	echo json_encode($data,JSON_UNESCAPED_UNICODE);
		// 	    	exit;
		// 		}
		// 		//添加用户
		// 		$new_user = $this->Model_m->insert('users',$data);

		// 		$user_data_where = array('user_id'=>$data['user_id']); //where
		//         $user_weixin_where = array('user_id'=>$data['user_id']); //where
		//         $user_data['success'] = $this->Model_m->data_success('4000');
		//     	$user_data['user'] = $this->Model_m->select('users',$user_data_where);
		//     	$user_data['weixin'] = $this->Model_m->select('users_weixin',$user_weixin_where);
		    		
		//     	if($user_data['user'][0]['user_id']){
		//     		echo json_encode($user_data,JSON_UNESCAPED_UNICODE);
		//     	}
		// 		exit;
		// 	}
		// }

		
		// //修改用户**
		// if(IS_PUT){
		// 	//如果是PUT提交
		// 	$user_id = isset($_REQUEST['user_id'])?$_REQUEST['user_id']:'';
		// 	$user_where = array('user_id'=>$user_id); //where
		// 	$user = $this->Model_m->select('users',$user_where);

		// 	if(empty($user)){
		// 		$data['error'] = $this->Model_m->data_error('4006');
		//     	echo json_encode($data,JSON_UNESCAPED_UNICODE);
		//     	exit;
		// 	}else{

		// 		$data['user_id'] = isset($_REQUEST['user_id'])?$_REQUEST['user_id']:'';
		// 		$data['username'] = isset($_REQUEST['username'])?$_REQUEST['username']:'';
		// 		$data['password'] = isset($_REQUEST['password'])?MD5($_REQUEST['password']):'';
		// 		$data['mobile'] = isset($_REQUEST['mobile'])?$_REQUEST['mobile']:'';
		// 		$data['email'] = isset($_REQUEST['email'])?$_REQUEST['email']:'';
					
		// 		//修改用户

		// 		$user_update = $this->Model_m->update('users',$user_where,$data);
				
		// 		$user_data_where = array('user_id'=>$data['user_id']); //where
		//         $user_weixin_where = array('user_id'=>$data['user_id']); //where
		//         $user_data['success'] = $this->Model_m->data_success('4000');
		//     	$user_data['user'] = $this->Model_m->select('users',$user_data_where);
		//     	$user_data['weixin'] = $this->Model_m->select('users_weixin',$user_weixin_where);
		    			
		//     	if($user_data['user'][0]['user_id']){
		//     		echo json_encode($user_data,JSON_UNESCAPED_UNICODE);
		//     	}
		// 	}
		// 	exit;
		// }

		// //删除用户**
		// if(IS_DEL){
		// 	//如果是IS_DEL提交
		// 	$user_id = isset($_REQUEST['user_id'])?$_REQUEST['user_id']:'';
		// 	$user_where = array('user_id'=>$user_id); //where
		// 	$user = $this->Model_m->select('users',$user_where);

		// 	if(empty($user)){
		// 		$data['error'] = $this->Model_m->data_error('4006');
		//     	echo json_encode($data,JSON_UNESCAPED_UNICODE);
		//     	exit;
		// 	}else{
		// 		//修改用户
		// 		$user_delete = $this->Model_m->delete('users',$user_where);
		//         $user_data['success'] = $this->Model_m->data_success('4000');
		//     	echo json_encode($user_data,JSON_UNESCAPED_UNICODE);
		    	
		// 	}
		// 	exit;
		// }


	}

	
    
}
    

